﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace LinqBasics
{
    class Program1
    {
        static void Main1(string[] args)
        {
            string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
            //var shortWords = from word in words
            //                 where word.Length <= 5
            //                 select word;

            //shortWords.ToList().ForEach(x => Console.WriteLine(x));

            //var groups = from word in words
            //             orderby word ascending
            //             group word by word.Length into lengthGroups
            //             orderby lengthGroups.Key ascending
            //             select new { Length = lengthGroups.Key, Words = lengthGroups };

            //foreach (var group in groups)
            //{
            //    Console.WriteLine("Words of length " + group.Length);
            //    foreach (string word in group.Words)
            //        Console.WriteLine(" " + word);
            //}



            Book[] books = new Book[] {
            new Book("Ajax in Action", "Manning", 2005),
            new Book("Windows Forms in Action", "Manning", 2006),
            new Book("RSS and Atom in Action", "Manning", 2006)
            };


            //XElement xml = new XElement("books",
            //    from book in books
            //    where book.Year == 2006
            //    select new XElement("book",
            //        new XAttribute("title", book.Title),
            //        new XElement("publisher", book.Publisher)));

            //Console.WriteLine(xml);

            //var newType = new
            //{
            //    Name = "ritesh",
            //    ID = 1,
            //    BirthDate = new { BirthMonth = DateTime.Now.Month }
            //};

            //GetBooksByCriteria((Book book) => book.Year > 2004, books.ToList<Book>())
            //    .OrderByDescending(book => book.Publisher.Length)
            //    .Take(1).ToList()                
            //    .ForEach(book => { Console.WriteLine("{0}/{1}", book.Title, book.Year); });

            //foreach (var item in Iterate())
            //{
            //    Console.WriteLine(item);
            //}

            //int[] numbers = { 1, 2, 3 };

            

            //var query =
            //from n in numbers
            //select Square(n);
            
            //foreach (var n in query.ToList())
            //    Console.WriteLine(n);

            XNamespace nms = "http://www,turpin-distribution.com";

            XDocument doc = new XDocument(
                // To use a XElement the XDeclaration and XCooment can't be used
                //XElement doc = new XElement(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("A sample xml file"),
                new XElement(nms + "employees",
                    new XElement("employee",
                        new XAttribute("id", 1),
                        new XAttribute("salaried", "false"),
                            new XElement("name", "Gustavo Achong"),
                            new XElement("hire_date", "7/31/1996")),
                    new XElement("employee",
                        new XAttribute("id", 3),
                        new XAttribute("salaried", "true"),
                            new XElement("name", "Kim Abercrombie"),
                            new XElement("hire_date", "12/12/1997")),
                    new XElement("employee",
                        new XAttribute("id", 8),
                        new XAttribute("salaried", "false"),
                            new XElement("name", "Carla Adams"),
                            new XElement("hire_date", "2/6/1998")),
                    new XElement("employee",
                        new XAttribute("id", 9),
                        new XAttribute("salaried", "false"),
                            new XElement("name", "Jay Adams"),
                            new XElement("hire_date", "2/6/1998"))
                    )
                );

            IEnumerable<XElement> salariedEmployees =   from elem in doc.Descendants("employee")
                                                        where elem.Attribute("salaried").Value == "true"
                                                        select elem;

            salariedEmployees.ToList<XElement>().ForEach(xElement => Console.WriteLine(xElement));


            //doc.Descendants("employee").Where(employee => employee.Attribute("salaried").Value == "true");

            

            Console.ReadLine();
        }

        static double Square(double n)
        {
            Console.WriteLine("Computing Square(" + n + ")...");
            return Math.Pow(n, 2);
        }

        static IEnumerable<Int32> Iterate()
        {
            yield return 1;
            yield return 2;
            yield return 3;
        }

        delegate Boolean SelectionCriteria(Book book, Boolean someShit);

        static List<Book> GetBooksByCriteria(SelectionCriteria selectionCriteria, List<Book> books)
        {            
            throw new NotImplementedException();
        }



        //static List<Book> GetBooksByCriteria(Func<Book, Boolean> selectionCriteria, List<Book> books)
        //{
        //    throw new NotImplementedException();
        //}

        static List<Book> GetBooksByCriteria(Predicate<Book> criteriaCheck, List<Book> books)
        {
            List<Book> matchingBooks = new List<Book>();
            books.ForEach(book => { if (criteriaCheck(book)) matchingBooks.Add(book); });
            return matchingBooks;

            
        }

        
    }

    class Book
    {
        public string Publisher;
        public string Title;
        public int Year;
        public Book(string title, string publisher, int year)
        {
            Title = title;
            Publisher = publisher;
            Year = year;
        }
    }

    public static class CommonExtensionMethods
    {
        public static string ToRitesh(this string toRitesh)
        {
            return toRitesh.ToString();
        }
    }

    


}
